package mil.af.amc.adpetracker.web.actions.HandReceipt;

import mil.af.amc.adpetracker.web.actions.AbstractAction;
import mil.af.amc.adpetracker.service.HandReceiptService;
import mil.af.amc.adpetracker.service.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Action for closing out a HandReceipt
 *
 * @author TSgt Ferguson
 */
public class CloseHandReceiptAction extends AbstractAction {

    public static final Log log = LogFactory.getLog(CloseHandReceiptAction.class);

    private HandReceiptService handReceiptService;

    public void setHandReceiptService(HandReceiptService handReceiptService) {
        this.handReceiptService = handReceiptService;
    }

    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int id = Integer.parseInt(httpServletRequest.getParameter("hrId"));

        try {
            handReceiptService.closeHandReceipt(id);
        } catch (ServiceException e) {
            log.debug("handReceiptService.closeHandReceipt() threw ServiceException", e);

            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
        }

        return actionMapping.findForward("success");

    }
}
